package top.jnaw.jee.platform.controller.basics.extend;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.DepartmentCallBack;
import top.jnaw.jee.platform.model.Files;
import top.jnaw.jee.platform.model.UserAnalysis;
import top.jnaw.jee.platform.model.UserLocal;
import top.jnaw.jee.platform.model.Users;
import top.jnaw.jee.platform.model.WechatUsers;
import top.jnaw.jee.utils.Cache;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Models;
import top.jnaw.jee.utils.Shiros;
import top.jnaw.jee.utils.Strings;
import top.jnaw.jee.utils.nim.NIMSMS;
import top.jnaw.jee.utils.nim.Utils;

public class UsersController extends Controller {

  public void add() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final String mobile = Strings.getString(req, "mobile", "");
    final String name = Strings.getString(req, "name", "");
    if (StrKit.notBlank(mobile)) {
      result = Users.add(mobile, name);
    }
    renderJson(result.toJson());
  }

  public void captcha() {
    JSONObject req = Strings.toJSONObject(this);
    final String mobile = Strings.getString(req, "mobile", "");

    Result result = Result.kBadParam;
    String captcha = null;
    if (StrKit.notBlank(mobile)) {
      captcha = NIMSMS.captcha(mobile);
      Cache.set(mobile, captcha);
      result = null != captcha ? Result.kSuccess : Result.kException;
    }

    renderJson(Models.toJson(result, Utils.md5(captcha)));
  }

  public void del() {
    ModelResult result = new ModelResult(Result.kModelDeleteError);
    JSONObject req = Strings.toJSONObject(this);
    JSONArray ids = Strings.getJSONArray(req, "ids", null);
    if (null != ids && ids.size() > 0) {
      for (int i = 0; i < ids.size(); i++) {
        if (Users.del(ids.getInteger(i))) {
          result.setResult(Result.kSuccess);
        }
      }
    }
    renderJson(result.toJson());
  }

//  public void isAuthc() {
//    ModelResult result = new ModelResult(Result.kBadParam);
//    JSONObject req = Strings.toJSONObject(this);
//    final String version = Strings.getString(req, "version", "");
//    final String address = Strings.getString(req, "address", "");
//    final Object longitudeD = Strings
//        .getObject(req, "longitude", Consts.DEFAULT_DOUBLE);
//    final Object latitudeD = Strings
//        .getObject(req, "latitude", Consts.DEFAULT_DOUBLE);
//    final String manufacturer = Strings.getString(req, "manufacturer", "");
//    final String model = Strings.getString(req, "model", "");
//    final boolean back_mode = Strings.getboolean(req, "back_mode", false);
//    final String type = Strings.getString(req, "type", "app");
//
//    if (Shiros.isAuthc()) {
//      Users user = Users.getByUsername(Shiros.getUser());
//      result = UserAnalysis
//          .add(user.getId(), version, address,
//              Double.parseDouble(longitudeD.toString()),
//              Double.parseDouble(latitudeD.toString()),
//              manufacturer, model, back_mode, type, getHeader("User-Agent"),
//              false);
//    } else {
//      result.setResult(Result.kUnLogin);
//    }
//    renderJson(result.toJson());
//  }

  public void list() {
    JSONObject req = Strings.toJSONObject(this);
    final int page = Strings
        .getInt(req, "page", Consts.DEFAULT_PAGE);
    final int size = Strings
        .getInt(req, "size", Consts.DEFAULT_PAGE_SIZE);
    final String keyWord = Strings.getString(req, "keyWord", "");
    renderJson(Strings
        .formatJsonDateTime(
            Users.list(page, size, keyWord)));
  }

  public void login() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final String mobile = Strings.getString(req, "mobile", "");
    final String password = Strings.getString(req, "password", "");
    if (StrKit.notBlank(mobile, password)) {
      Users user = Users.getByMobile(mobile);
      if (null != user) {
        if (Shiros.login(user.getUsername(), password)) {
          result.setObject(user);
          result.setResult(Result.kSuccess);
        }
      } else {
        result.setResult(Result.kNull);
      }
    }
    renderJson(result.toJson());
  }

  public void logout() {
    ModelResult result = new ModelResult(Result.kBadParam);
    if (Shiros.isAuthc()) {
      boolean r = Shiros.logout();
    }
    result.setResult(Result.kSuccess);
    renderJson(result.toJson());
  }

  public void query() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    String openid = Strings.getString(req, "openid", "");
    if (StrKit.notBlank(openid)) {
      WechatUsers user = WechatUsers.getByOpenid(openid);
      result.setObject(user);
      result.setResult(Result.kSuccess);
    }
    renderJson(result.toJson());
  }

  public void queryAdmin() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);
    if (id > 0) {
      Users user = Users.query(id);
      result.setObject(user);
      result.setResult(Result.kSuccess);
    }
    renderJson(result.toJson());
  }

  public void queryLocal() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    Users users = Users.getByUsername(Shiros.getUser());
    int uid = users != null ? users.getId() : Consts.DEFAULT_BAD_ID;
    String type = Strings.getString(req, "type", "");
    if (uid > 0 && StrKit.notBlank(type)) {
      result = UserLocal.query(uid, type);
    }
    renderJson(result.toJson());
  }

  public void updateLocal() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    Users users = Users.getByUsername(Shiros.getUser());
    int uid = users != null ? users.getId() : Consts.DEFAULT_BAD_ID;
    String json = Strings.getString(req, "json", "");
    String type = Strings.getString(req, "type", "");
    if (uid > 0 && StrKit.notBlank(type)) {
      if (UserLocal.update(uid, json, type)) {
        result.setResult(Result.kSuccess);
      }
    }
    renderJson(result.toJson());
  }


  public void update() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);
    final String mobile = Strings.getString(req, "mobile", "");
    final String name = Strings.getString(req, "name", "");
    if (StrKit.notBlank(mobile)) {
      result = Users.update(id, mobile, name);
    }
    renderJson(result.toJson());
  }

  public void updatePwd() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final String mobile = Strings.getString(req, "mobile", "");
    final String password = Strings.getString(req, "password", "");
    final String captcha = Strings.getString(req, "captcha", "");
    if (NIMSMS.verifyCaptcha(mobile, captcha)) {
      result = Users.updatePwd(mobile, password);
    }
    renderJson(result.toJson());
  }

//  public void importExcel() {
//    ModelResult result = new ModelResult(Result.kBadParam);
//    JSONObject req = Strings.toJSONObject(this);
//    final int fid = Strings.getInt(req, "fid", Consts.DEFAULT_BAD_ID);
//    if (fid > 0) {
//      Files file = Files.dao.findById(fid);
//      int start = file.getUrl().indexOf("top/") + 15;
//      int end = file.getUrl().length();
//
//      String path = Consts.UPLOAD_QINIU + file.getUrl()
//          .substring(start, end);
//
//      result = new DepartmentCallBack().foo(path, 0);
//    }
//
//    renderJson(result.toJson());
//  }
}
